package com.wnhz.work.jy.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.LogisticsAdapter;
import com.wnhz.work.jy.bean.LogisticsBean;
import com.wnhz.work.jy.bean.OrderBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物流
 */
public class LogisticsActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "LogisticsActivity";
    private String orderId;
    private TextView title;
    private TextView ordrt_start;//订单状态
    private TextView source;//配送方式
    private TextView waybill_number;//运单编号
    private TextView phone_official;//官方电话
    private TextView good_num;//商品数量
    private ImageView good_img;//商品图片

    private ListView listView;
    private LogisticsAdapter adapter;
    private List<LogisticsBean> list = new ArrayList<>();
    private OrderBean orderBean;
    private View lay;
    private String orderState;
    private View linear;
    private ScrollView scrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);
        orderId = getIntent().getStringExtra("orderId");
        orderState = getIntent().getStringExtra("OrderState");
        initTitle();
        ordrt_start = (TextView) findViewById(R.id.ordrt_start);
        source = (TextView) findViewById(R.id.source);
        waybill_number = (TextView) findViewById(R.id.waybill_number);
        phone_official = (TextView) findViewById(R.id.phone_official);
        good_num = (TextView) findViewById(R.id.good_num);
        good_img = (ImageView) findViewById(R.id.good_img);
        listView = (ListView) findViewById(R.id.listView);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        linear = findViewById(R.id.linear);
        lay = findViewById(R.id.lay);
        adapter = new LogisticsAdapter(this,list);
        listView.setAdapter(adapter);
        getData();
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("物流信息");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private void getData(){
        Map<String,Object> map = new HashMap<>();
        map.put("orderId",orderId);
        showDialog();
        XUtil.Post(ConfigUtil.EXPRESS,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+orderId   +"    "+result );
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray array = jsonObject.optJSONArray("express");
                    String expressName = jsonObject.optString("expressName");
                    String expressNo = jsonObject.optString("expressNo");
                    String orderNums = jsonObject.optString("orderNums");
                    String goodsImg = jsonObject.optString("goodsImg");
                    list.clear();
                    if (array != null){
                        for (int i = 0; i < array.length(); i++) {
                            LogisticsBean bean = new LogisticsBean();
                            bean = gson.fromJson(array.optString(i),LogisticsBean.class);
                            list.add(bean);
                        }
                    }
                    lay.setVisibility(View.VISIBLE);
                    setData(expressName, expressNo,orderNums,goodsImg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    private void setData(String expressName, String expressNo,String orderNums,String goodsImg) {
        waybill_number.setText(expressNo);
        source.setText(expressName);
        x.image().bind(good_img,goodsImg);
        good_num.setText(orderNums+"件商品");
        switch (Integer.parseInt(orderState)){
            case 0:
                ordrt_start.setText("待付款");
                break;
            case 1:
                ordrt_start.setText("待发货");
                break;
            case 2:
                ordrt_start.setText("待收货");
                break;
            case 3:
                ordrt_start.setText("待评价");
                break;
            case 4:
                ordrt_start.setText("交易完成");
                break;
            case 5:
                ordrt_start.setText("取消订单");
                break;
            case 6:
                ordrt_start.setText("退款");
                break;
        }
        if(list.size() == 0){
            linear.setVisibility(View.GONE);
        }else {
            linear.setVisibility(View.VISIBLE);
        }
        adapter.notifyDataSetChanged();
        scrollView.scrollTo(0,0);
        scrollView.smoothScrollTo(0,0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
